package service

import (
	"sync"
	"time"

	"gitee.com/night-tc/gobige/logger"
	"gitee.com/night-tc/gobige/msgdef"
	"gitee.com/night-tc/gobige/threads"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/mem"
)

// 操作子协程的管理器
func (this *LoginService) GetThreadGo() *threads.ThreadGo {
	return this.thgo
}

// 获取服务器当前繁忙状态0-100
func (this *LoginService) GetLoad() int {
	var c int
	var vm int

	if loads, err := cpu.Percent(0, false); err == nil {
		if len(loads) > 0 {
			c = int(loads[0])
		}
	} else {
		logger.Error(err)
		c = 100
	}

	if memorys, err := mem.VirtualMemory(); err == nil {
		vm = int(memorys.UsedPercent)
	} else {
		logger.Error(err)
		vm = 100
	}

	if c > vm {
		return c
	}

	return vm
}

func (this *LoginService) GetNowTime() time.Time {
	return this.NowTime.Load()
}

// 服务器是否可用
func (this *LoginService) IsSrvValid() bool {
	return true
}

// 服务器不可用的时候，的处理函数
func (this *LoginService) HandlerSrvInvalid(eid uint64) {

}

// 获取新的EntityID
func (this *LoginService) GetNewEID() uint64 {
	return this.eIDGenerater.GenerateID()

}

// 获取等待回复的map
func (this *LoginService) GetWaitMap() *sync.Map {
	return &this.WaitList
}

// 发消息给其他服务器的对象
func (this *LoginService) PostMsgToEntity(srvid, spid, eid uint64, msg msgdef.IMsg) (err error) {
	return nil
}

// 通过目标服务器发消息给客户端
func (this *LoginService) PostMsgToClient(srvid, spid, eid uint64, msg msgdef.IMsg) (err error) {
	return nil
}

// 通过目标服务器发消息给客户端
func (this *LoginService) PostMsgToClientBuf(srvid, spid, eid uint64, data []byte) (err error) {
	return nil
}

// 发消息到目标服务器
func (this *LoginService) PostSess(srvid uint64, msg msgdef.IMsg) {
}
